package com.amkai.mobile.client.common;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.amkai.mobile.bean.organization.MobileBusinessEntity;
import com.amkai.mobile.bean.organization.MobileBusinessGroup;
import com.amkai.mobile.bean.passport.MobilePassport;
import com.amkai.mobile.common.job.login.MobileLoginJob;
import com.amkai.mobile.common.job.login.MobileLoginJobResult;
import com.amkai.mobile.common.job.organization.MobileOrganizationJob;
import com.amkai.mobile.common.job.organization.MobileOrganizationJobResult;
import com.amkai.mobile.utils.data.Constants;
import com.amkai.mobile.utils.data.MobileDb;
import com.amkai.mobile.utils.manager.job.MobileServiceManager;

public class SharedDataProvider {
	private MobileOrganizationJobResult mojr = null;
	MobileLoginJobResult loginJobResult;

	private static SharedDataProvider sharedDataProvider;
	
	public static void reset(){
		sharedDataProvider = null;
	}

	public static SharedDataProvider getInstance() {
		if (sharedDataProvider == null) {
			sharedDataProvider = new SharedDataProvider();
		}
		return sharedDataProvider;
	}

	public MobileOrganizationJobResult getMojr(Activity context) throws Exception {
		if (mojr == null) {
				mojr = MobileServiceManager.processMobileJob(context, MobileOrganizationJob.getOrganizationJobToLoginInfo());
		}
		return mojr;
	}

	public Long getPreferedBusinessEntityId(Context context) {
		Long result = null;

		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);

		String bg = settings.getString("preferences_be", "");
		
		if ( bg != null && !bg.equals("") ) {
			result = new Long(bg);
		}
		return result;
	}

	public String getConnectionParameter(Context context, String parameterName) {
		String result = "";
		String value = MobileDb.getLoginParameter(context, parameterName);

		if (parameterName.equals(Constants.SERVER)) {
			if (value.length() != 0) {
				result = Constants.URLPrefix + value + Constants.URLSuffix;
			}
		} else {
			result = value;
		}
		return result;
	}

	public Long getPassportId(Context context) {
		Long result = null;

		try {

			result = getPassport(context).id;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}

	public String getPassportName(Activity context) {
		String result = null;

		try {

			result = getPassport(context).userName;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}
	
	public MobilePassport getPassport(Context context) {
		MobilePassport result = null;

		try {

			result = getMobileLoginJobResult(context).getPassport();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}

	
	MobileLoginJobResult getMobileLoginJobResult(Context context) {
		if (loginJobResult == null) {
			String username = getConnectionParameter(context, Constants.USERNAME);
			String password = getConnectionParameter(context, Constants.PASSWORD);
			try {
				loginJobResult = MobileServiceManager.processMobileJob(context, MobileLoginJob.getByUsenameAndPassword(username, password));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return loginJobResult;
	}

	public Boolean getPreferedAutoLogin(Activity context) {
		Boolean result = null;
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getBaseContext());

		Boolean bg = settings.getBoolean("autologin", true);
		if (bg != null) {
			result = new Boolean(bg);
		}
		return result;
	}

	public String getBeNameById(Activity context, Long currentBEId) throws Exception {
		String result = null;

		for ( MobileBusinessGroup bg : getMojr(context).businessGroups) {
			for(MobileBusinessEntity be : bg.businessEntities){
				if (be.id.equals(currentBEId)){
					result = be.shortName;
					break;
				}
			}
		}
		return result;
	}

	public void setAppTitle(Activity context, Long currentBe) {
		try {
			context.setTitle(AEMobileConstants.APP_TITLE  +" - "+ getBeNameById(context,currentBe) + " [" + getPassportName(context)+"]");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
	}
	
	

}
